package net.pantolomin.nayo;

import java.nio.channels.SelectionKey;
import java.nio.channels.spi.AbstractSelectableChannel;

/**
 * Represents a request to change a selector
 * 
 * @param <SC>
 *          the type of selectable channel
 */
class ChangeRequest<SC extends AbstractSelectableChannel> {
	/** The channel */
	private final SC channel;

	/** The type of change */
	private final ChangeType type;

	/** The selection key */
	private final SelectionKey selectionKey;

	/**
	 * Create the change request
	 * 
	 * @param channel
	 *          the channel
	 * @param type
	 *          the type of change
	 */
	public ChangeRequest(SC channel, ChangeType type) {
		this(channel, type, null);
	}

	/**
	 * Create the change request
	 * 
	 * @param channel
	 *          the channel
	 * @param type
	 *          the type of change
	 * @param selectionKey
	 *          the selection key
	 */
	public ChangeRequest(SC channel, ChangeType type, SelectionKey selectionKey) {
		this.channel = channel;
		this.type = type;
		this.selectionKey = selectionKey;
	}

	/**
	 * @return the socket
	 */
	public SC getChannel() {
		return this.channel;
	}

	/**
	 * @return the type of change
	 */
	public ChangeType getType() {
		return this.type;
	}

	/**
	 * @return the selection key
	 */
	public SelectionKey getSelectionKey() {
		return this.selectionKey;
	}

	/**
	 * The type of change
	 */
	public static enum ChangeType {
		/** Register a server socket to accept connections */
		REGISTER_ACCEPT,
		/** Register a client socket to intercept the connection's end */
		REGISTER_CONNECT,
		/** Ask the selector to listen for incoming data */
		ASK_FOR_READ,
		/** Ask the selector when it is ready to write */
		ASK_FOR_WRITE,
		/** Declare a timeout for a connection attempt */
		DECLARE_TIMEOUT,
		/** Close a connection */
		CLOSE_CONNECTION
	}
}